package cn.weiyang.controller;

import cn.weiyang.model.Order;
import cn.weiyang.model.OrderRuselt;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单业务控制器
 * Created by Administrator on 2017/7/24.
 */
/*RestController相当于规定了返回值都是json格式的数据*/
@RestController
@RequestMapping("app/api/v3/orders")
public class OrderController {
    /*利用log4j进行测试*/
    private static Logger logger= LogManager.getLogger(OrderController.class);
    @RequestMapping("/test")
    public ModelAndView getIndex(){

        return new ModelAndView("test");
    }
    /**
     * 根据订单编号查询订单(PathVariable表示从网址中传值)
     * @return
     */
    @RequestMapping(value = "/{orderid}", method= RequestMethod.GET)
    public OrderRuselt<?> getOrder(@PathVariable String orderid){
        logger.info(orderid);
        /*抛出的异常由全局异常处理类进行处理*/
        if("110".equals(orderid)){
            throw new ServiceException(Errcode.Gl_SYS_BUSY);
        }
        Order order=new Order();
        order.setOrderid(1);
        order.setPrice(5423);
        order.setGoodnum(54);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("order",order);
        OrderRuselt<Map<String,Object>> orderRuselt=new OrderRuselt<Map<String, Object>>();
        orderRuselt.setData(map);
        return orderRuselt;

    }

    /**
     * 删除指定订单
     * @param orderid
     * @return
     */
    @RequestMapping(value = "/{orderid}", method= RequestMethod.DELETE)
    public OrderRuselt<?> delOrder(@PathVariable String orderid){
        logger.info(orderid);
        if("0".equals(orderid)){
            throw new NullPointerException();
        }
        OrderRuselt<Void> orderRuselt=new OrderRuselt<Void>();
        return orderRuselt;
    }

    /**
     * 增加订单（RequestParam表示从网页中传值，默认值为true必须传，如果设为false，可以不传值）
     * @param price
     * @param goodnum
     * @return
     */
    @RequestMapping(value = "", method= RequestMethod.POST)
    public OrderRuselt<?> addOrder( @RequestParam Double price,@RequestParam Integer goodnum){
        OrderRuselt<Void> orderRuselt=new OrderRuselt<Void>();
        return orderRuselt;
    }

    /**
     * 修改订单(up相当于给orderid起了个别名)
     * @param orderid
     * @return
     */
    @RequestMapping(value = "/{up}", method = RequestMethod.PUT)
    public OrderRuselt<?> updateOrder(@PathVariable("up") String orderid){
        System.out.println(orderid);
        OrderRuselt<Void> orderRuselt=new OrderRuselt<Void>();
        return orderRuselt;
    }

}
